namespace TSL.Utility.Json
{
    using System;
    using System.Configuration;
    using System.Text;
    using System.Web.Profile;

    public class ProfileBaseConverter : IJavaScriptConverter
    {
        private string clientType = "Ajax.Web.Profile";

        public ProfileBaseConverter()
        {
            base.m_serializableTypes = new Type[] { typeof(ProfileBase) };
        }

        public override string GetClientScript()
        {
            if (Utility.Settings.OldStyle.Contains("renderJsonCompliant"))
            {
                return "";
            }
            return (JavaScriptUtil.GetClientNamespaceRepresentation(this.clientType) + "\r\n" + this.clientType + " = function() {\r\n\tthis.toJSON = function() {\r\n\t\tthrow \"" + this.clientType + " cannot be converted to JSON format.\";\r\n\t};\r\n\tthis.setProperty_callback = function(res) {\r\n\t};\r\n\tthis.setProperty = function(name, object) {\r\n\t\tthis[name] = object;\r\n\t\tAjaxPro.Services.Profile.SetProfile({name:o}, this.setProperty_callback.bind(this));\r\n\t};\r\n};\r\n");
        }

        public override string Serialize(object o)
        {
            StringBuilder sb = new StringBuilder();
            this.Serialize(o, sb);
            return sb.ToString();
        }

        public override void Serialize(object o, StringBuilder sb)
        {
            if (!(o is ProfileBase))
            {
                throw new NotSupportedException();
            }
            ProfileBase base2 = (ProfileBase) o;
            bool flag = true;
            if (!Utility.Settings.OldStyle.Contains("renderJsonCompliant"))
            {
                sb.Append("Object.extend(new " + this.clientType + "(), ");
            }
            sb.Append("{");
            foreach (SettingsProperty property in ProfileBase.Properties)
            {
                if (!flag)
                {
                    sb.Append(",");
                }
                JavaScriptUtil.QuoteString(property.Name, sb);
                sb.Append(":");
                JavaScriptSerializer.Serialize(base2[property.Name], sb);
                flag = false;
            }
            sb.Append("}");
            if (!Utility.Settings.OldStyle.Contains("renderJsonCompliant"))
            {
                sb.Append(")");
            }
        }
    }
}

